//
//  YsCloudFileDownloadModel.m
//  YsCloudFileManageSDK
//
//  Created by 宋宏康 on 2020/2/27.
//

#import "YsCloudFileDownloadModel.h"
#import "YsCloudFileManagerHeader.h"
#import "YsCloudFileRealmManager.h"
#import "NSString+YscCloudFileTools.h"
#import "YsFileUtil.h"
#import <YYKit/YYKit.h>

@implementation YsCloudFileDownloadModel

@synthesize tmpPath = _tmpPath;

- (NSString *)description
{
    return [self modelDescription];
}

#pragma mark - init

- (instancetype)initWithIsInitRealmModel:(BOOL)isInit
{
    self = [super init];
    if (self) {
        if (isInit) {
            self.downloadReamModel = [[YsCloudFileDownloadRealmModel alloc] init];
        }
    }
    return self;
}

- (instancetype)initWithProductDownloadModel:(BOOL)productLoadModel
{
    self = [super init];
    if (self) {
        if (productLoadModel) {
            self.downloadReamModel = [[YsCloudFileDownloadRealmModel alloc] init];
            self.downloadReamModel.accountCode = kAccountCode;
            self.downloadReamModel.downloadID = self.downloadReamModel.incrementaID;
            self.downloadReamModel.createTime = NSString.currentTimeStr;
        }
    }
    return self;
}

#pragma mark - setter
- (void)setRequestUrl:(NSString *)requestUrl
{
    _requestUrl = requestUrl;
    if (self.downloadReamModel) {
        [YsCloudFileRealmManager updateObj:^{
            self.downloadReamModel.requestUrl = requestUrl;
        }];
    }
}


- (void)setTmpPath:(NSString *)tmpPath
{
    if ([tmpPath containsString:YsFileUtil.getTmpPath]) {
        _tmpPath  = tmpPath;
    }else{
        /// 先把后面相同的截取出来
      NSString *samePath = [tmpPath substringFromIndex:YsFileUtil.getTmpPath.length];
      _tmpPath = [NSString stringWithFormat:@"%@%@",YsFileUtil.getTmpPath,samePath];
    }

    if (self.downloadReamModel) {
        [YsCloudFileRealmManager updateObj:^{
            self.downloadReamModel.tmpPath = tmpPath;
        }];
    }
}

- (NSString *)tmpPath
{
    if ([_tmpPath containsString:YsFileUtil.getHomePath]) {
        return _tmpPath;
    }else{
        /// 先把后面相同的jie
        NSString *samePath = [_tmpPath substringFromIndex:YsFileUtil.getHomePath.length];
        return [NSString stringWithFormat:@"%@%@",YsFileUtil.getHomePath,samePath];
    }
}

- (void)setCachePath:(NSString *)cachePath
{
    _cachePath = cachePath;

    if (self.downloadReamModel) {
        [YsCloudFileRealmManager updateObj:^{
            self.downloadReamModel.cachePath = cachePath;
        }];
    }
}

- (void)setCustomCacheName:(NSString *)customCacheName
{
    _customCacheName = customCacheName;
    if (self.downloadReamModel) {
        [YsCloudFileRealmManager updateObj:^{
            self.downloadReamModel.customCacheName = customCacheName;
        }];
    }
}

- (void)setTotalSize:(int64_t)totalSize
{
   if (self.downloadReamModel && totalSize != _totalSize) {
       [YsCloudFileRealmManager updateObj:^{
              self.downloadReamModel.totalSize = totalSize;
        }];
   }
    _totalSize = totalSize;
}

- (void)setProgress:(CGFloat)progress
{
    /// 手动触发kvo
    [self willChangeValueForKey:NSStringFromSelector(@selector(progress))];
    _progress = progress;
    
    if (progress == 0) {
        NSLog(@"111111");
        NSLog(@"111111");
        NSLog(@"111111");
    }
    
    if (self.downloadReamModel) {
        [YsCloudFileRealmManager updateObj:^{
           self.downloadReamModel.progress = progress;
         }];
    }
    [self didChangeValueForKey:NSStringFromSelector(@selector(progress))];
}

- (void)setDownloadStatus:(int)downloadStatus
{
    _downloadStatus = downloadStatus;
    if (downloadStatus == 5) {
        NSLog(@"展厅");
    }
    
    if (downloadStatus == 3) {
        NSLog(@"正在下载哦");
        NSLog(@"111111");
    }
    
    
    if (self.downloadReamModel) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [YsCloudFileRealmManager updateObj:^{
                self.downloadReamModel.downloadStatus = downloadStatus;
            }];
        });        
    }
}

- (void)setLastPathComponent:(NSString *)lastPathComponent
{
    _lastPathComponent = lastPathComponent;

    if (self.downloadReamModel) {
        [YsCloudFileRealmManager updateObj:^{
            self.downloadReamModel.mime = lastPathComponent;
        }];
    }
}

- (void)setWaitTaskTime:(NSString *)waitTaskTime
{
    _waitTaskTime = waitTaskTime;
    if (self.downloadReamModel) {
          [YsCloudFileRealmManager updateObj:^{
              self.downloadReamModel.waitTaskTime = waitTaskTime;
          }];
      }
}
@end
